//
//  HelpViewController.m
//  CardWarrior
//
//  Created by Tekka Xie on 12-4-22.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import "HelpViewController.h"
#import "AudioManager.h"

@implementation HelpViewController

@synthesize backgroundA;
@synthesize backgroundB;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    backgroundA = (UIImageView*)[self.view viewWithTag:101];
    backgroundB = (UIImageView*)[self.view viewWithTag:102];

    backgroundA.image = [UIImage imageNamed:@"Help-01.jpg"];
    backgroundB.image = [UIImage imageNamed:@"Help-02.jpg"];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight
        || interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        return YES;
    
    return NO;
}

#pragma mark - touches

static CGPoint startPoint;
static int curIndex = 1;
static BOOL isAnimate = NO;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches] anyObject];
    
    if (touch.view.tag == 100)
    {
        [self performSegueWithIdentifier:@"SegueToMainViewController" sender:self];
        [[AudioManager sharedInstance] PlaySFX:@"BackSFX"];
    }
    else
    {
        startPoint = [touch locationInView:self.view];
    }
}  

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
	CGPoint touchPoint = [touch locationInView:self.view];
    
    if (isAnimate == YES)
        return ;
    
    CGFloat diff = touchPoint.x - startPoint.x;
    
    if (diff < -35)
    {
        if (curIndex < 7)
        {
            curIndex++;
            [self leftForward];
        }
    }
    else if (diff > 35)
    {
        if (curIndex > 1)
        {
            curIndex--;
            [self rightForward];
        }
    }
}

- (void)leftForward
{
    isAnimate = YES;
    
    backgroundB.image = [UIImage imageNamed:[NSString stringWithFormat:@"Help-%02d.jpg",curIndex]];
    backgroundB.frame = CGRectMake(480, -2, 484, 324);
    
    [UIView animateWithDuration: 0.5  delay: 0.0
                        options: UIViewAnimationOptionCurveLinear
                     animations: ^(){
                        backgroundA.frame = CGRectMake(-484, -2, 484, 324);
                        backgroundB.frame = CGRectMake(-2, -2, 484, 324);
                     }
                     completion: ^(BOOL finished){
                            isAnimate = NO;
                            backgroundA.image = backgroundB.image;
                            backgroundA.frame = CGRectMake(-2, -2, 484, 324);
                            backgroundB.image = nil;
                     }
    ];
    
    [[AudioManager sharedInstance] PlaySFX:@"SelectSFX"];
}

- (void)rightForward
{
    isAnimate = YES;
    
    backgroundB.image = [UIImage imageNamed:[NSString stringWithFormat:@"Help-%02d.jpg",curIndex]];
    backgroundB.frame = CGRectMake(-484, -2, 484, 324);
    
    [UIView animateWithDuration: 0.5  delay: 0.0
                        options: UIViewAnimationOptionCurveLinear
                     animations: ^(){
                         backgroundA.frame = CGRectMake(480, -1, 484, 324);
                         backgroundB.frame = CGRectMake(-2, -1, 484, 324);
                     }
                     completion: ^(BOOL finished){
                             isAnimate = NO;
                             backgroundA.image = backgroundB.image;
                             backgroundA.frame = CGRectMake(-2, -2, 484, 324);
                             backgroundB.image = nil;
                     }
     ];
    
    [[AudioManager sharedInstance] PlaySFX:@"SelectSFX"];
}

@end
